/*
 * 创建日期 2006-5-30
 */
package com.jmcgg.freesky.common.util;

import javax.servlet.http.HttpSession;

import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: FreeSky Common Java Library <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2 <br>
 *          1.0.0, 2006-05-30, Initial Version
 */
public class SysUserManager {

	public SysUserManager() {
	}

	public static LoginUser getCurUser(HttpSession session) {
		return (LoginUser) session.getAttribute(Const.SYS_LOGINUSER);
	}

	public static String getCurUserID(HttpSession session) {
		LoginUser loginUser = (LoginUser) session
				.getAttribute(Const.SYS_LOGINUSER);
		if (loginUser == null)
			return null;
		return loginUser.getUserId();
	}

	public static String getCurSite(HttpSession session) {
		LoginUser loginUser = (LoginUser) session
				.getAttribute(Const.SYS_LOGINUSER);
		if (loginUser == null)
			return null;
		return loginUser.getSite();
	}

	public boolean validSuperUser(String userId, String password) {
		if (userId == null || password == null)
			return false;
		Configuration config = ConfigurationManager
				.getConfiguration("resources/freesky_common.xml");
		// zhouhmzju/A8
		String v_userId = config.getProperty("SysUserId",
				"dc70deaa96bf2dadb6ec577f23f34b23", "SysAdminUser");
		String v_password = config.getProperty("SysPassword",
				"596039809fe19dd0f046bdda6c2ebe74", "SysAdminUser");
		if (v_userId.equals(userId) && v_password.equals(password))
			return true;
		return false;
	}

	public static boolean isLogin(HttpSession session) {
		String userId = (String) session.getAttribute(Const.SYS_USER_ID);
		if (userId == null || userId.length() == 0)
			return false;
		return true;
	}

	public static boolean saveCurUser(String userId, String userName,
			String password, String site, HttpSession session) {
		LoginUser loginUser = new LoginUser(userId, password, site, userName);
		session.setAttribute(Const.SYS_LOGINUSER, loginUser);
		session.setAttribute(Const.SYS_USER_ID, userId);
		session.setAttribute(Const.SYS_USER_NAME, userName);
		session.setAttribute(Const.SYS_USER_PWD, password);
		// session.setAttribute(Const.SYS_LOGIN_SITE, site);
		return true;
	}

	public static void logout(HttpSession session) {
		session.removeAttribute(Const.SYS_LOGINUSER);
		session.removeAttribute(Const.SYS_USER_ID);
		session.removeAttribute(Const.SYS_USER_NAME);
		session.removeAttribute(Const.SYS_USER_PWD);
		// session.removeAttribute(Const.SYS_LOGIN_SITE);
	}
}
